var username;
var password;

function loginCheck() {
	var isPass = true;
	username = $("#username").val();
	password = $("#password").val();
	if(username.substring(6,12)!='000000'){
		if(username.substring(9,12)=='000'){
			$("#username").val(username.substring(0,12)+'34');
		} else if(username.substring(9,12)!='000'){
			$("#username").val(username.substring(0,12)+'35');
		}
	}
	username = $("#username").val();
	if (username == "" || password == "") {
		alert("用户名或密码不能为空!");
		return false;
	}
	DWREngine.setAsync(false);
	userDao.findUsername(username,function(data){
		if(data==false){
			alert("用户名不存在!");
			isPass = false;
		}else {
			isPass = true;
		}
	});
	if(!isPass){
		DWREngine.setAsync(true);
		return isPass;
	}
	userDao.findpassword(username,password,function(data){
		if(data==false){
			alert("密码输入错误，请重新输入!");
			isPass = false;
		}else {
			isPass = true;
		}
	});
	DWREngine.setAsync(true);
	return isPass;
}


$(function() {
	$(".login-detail").on("click", "#login-note", function() {
		var note = $("#note-container").html();
		var options = {
			animation : 700, // Animation speed
			buttons : {
				confirm : {
					action : function() {
						Apprise("close");
					}, 
					className : null, 
					id : "confirm", 
					text : "关闭" 
				}
			},
			input : false, // input dialog
			override : false
		};
		Apprise(note, options);
	});

	$(".login_cont , .login-regist").on("focus", "#username_show , #regist-username-show", function(event) {
		$(this).cityPicker({
			url : "/servlet/PickAreaServlet",
			buttons : true
		});
	});

	$(".login-detail").on("click", "#new-user", function() {
		// alert("用户注册");
		if ($(this).text() == "用户注册") {
			$(this).text("用户登录");
			$("#department,#regist-department").css("display","none");
		} else {
			$(this).text("用户注册");
			$("#department,#regist-department").css("display","none");
		}
		$(".login-regist").slideToggle("slow");
		$(".login_cont").slideToggle("slow");
		
		$(".username , .login-field").val("");
		//$('input[value='+unescape($.cookie('USER-TYPE'))+']').attr('checked',false);
		var radios = $('input[type=radio]')
		radios.each(function(i,item){
			$(item).attr('checked',false);
		});
	});

	$("input[type=radio]").on("click", function() {
		var username = $(".username").val();
		if(username==''){
			alert('请先选择用户名');
			return false;
		}
		var departmentId = $(this).val();
		if(departmentId==37){
			$("#department,#regist-department").css("display","inline");
			$('#password').val('');
			$(".username").val(username.substr(0, 12));
		}else{
			$("#department,#regist-department").css("display","none");
			$("#department,#regist-department").val("-1");
			if(departmentId==01||departmentId==34||departmentId==35){
				$(".username").val(username.substr(0, 12) + departmentId);
				if(unescape($.cookie('USER-NAME'))==$(".username").val()){
					if(unescape($.cookie('USER-PASSWORD'))!='null'){
						$('#password').val(unescape($.cookie('USER-PASSWORD')));
						document.getElementById("ck_savepass").checked=true;
					}
				} else {
					$('#password').val('');
					document.getElementById("ck_savepass").checked=false;
				}
			}
		}
	});
	$("#department,#regist-department").on("change",function(){
		var username = $(".username").val();
		var departmentId = $(this).val();
		$(".username").val(username.substr(0, 12) + departmentId);
		if(unescape($.cookie('USER-NAME'))==$(".username").val()){
			if(unescape($.cookie('USER-PASSWORD'))!='null'){
				$('#password').val(unescape($.cookie('USER-PASSWORD')));
				document.getElementById("ck_savepass").checked=true;
			}
		} else {
			$('#password').val('');
			document.getElementById("ck_savepass").checked=false;
		}
	});
	
	$('.login-regist input[type=text],.login-regist input[type=password]').focus(function(){
		$('#'+$(this).attr('id')+'-font').css('display','none');
	});	
	$('.login-regist input[type=radio]').focus(function(){
		$('#regist-type-font').css('display','none');
		if($(this).val()!=37){
			$('#regist-department-font').css('display','none');
		}
	});	
	$('#regist-department').focus(function(){
		$('#regist-department-font').css('display','none');
	});	
	
	
	if(unescape($.cookie('USER-NAME'))!='null'){
		$('#username').val(unescape($.cookie('USER-NAME')));
	}
	if(unescape($.cookie('USER-NAME-SHOW'))!='null'){
		$('#username_show').val(unescape($.cookie('USER-NAME-SHOW')));
	}
	if(unescape($.cookie('USER-TYPE'))!='null'){
		if(unescape($.cookie('USER-TYPE'))=='37'){
			if(unescape($.cookie('USER-DEPARTMENT'))!='null'){
				$("#department,#regist-department").css("display","inline");
				$('#department').val(unescape($.cookie('USER-DEPARTMENT')));
			}
		}
		$('input[value='+unescape($.cookie('USER-TYPE'))+']').attr('checked',true);
	}
	if(unescape($.cookie('USER-PASSWORD'))!='null'){
		$('#password').val(unescape($.cookie('USER-PASSWORD')));
	}
	if(unescape($.cookie('CHECKED-VALUE'))!='null'){
		$('#ck_savepass').attr("checked",unescape($.cookie('CHECKED-VALUE')));
	}		
	$('#ck_savepass').click(function(){
		if($('#ck_savepass').is(":checked")){
			var user_name=$('#username').val();
			var username_show = $('#username_show').val();
			var user_type = $("input:radio[name='type']:checked").val();
			var user_password = $('#password').val();
			var checkedValue = $('#ck_savepass').is(":checked");
			var departmentValue = $('#department').val();
			if(user_type==undefined){
				alert("请选择类型");
				return false;		
			}
			if(user_type=='37'){
				if(departmentValue=='-1'){
					alert('请选择部门');
					return false;
				}
			}
			if(user_name==""||user_password==""){
				alert("用户名或密码不能为空");
				return false;
			} else {
				$.cookie('USER-NAME', escape(user_name), { expires: 7 });
				$.cookie('USER-NAME-SHOW', escape(username_show), { expires: 7 });
				$.cookie('USER-TYPE', escape(user_type), { expires: 7 });
				$.cookie('USER-PASSWORD', escape(user_password), { expires: 7 });
				$.cookie('CHECKED-VALUE', escape(checkedValue), { expires: 7 });
				$.cookie('USER-DEPARTMENT', escape(departmentValue), { expires: 7 });
			}
		} else {
			$.cookie('USER-NAME', '', { expires: -1 });
			$.cookie('USER-NAME-SHOW', '', { expires: -1 });
			$.cookie('USER-TYPE', '', { expires: -1 });
			$.cookie('USER-PASSWORD', '', { expires: -1 });
			$.cookie('CHECKED-VALUE', '', { expires: -1 });
			$.cookie('USER-DEPARTMENT', '', { expires: -1 });
			/** 
				$('#username').val('');
				$('#username_show').val('');
				$('#type').val('-1');
				$('#password').val('');
			*/
		}
	});
});


function registCheck(){
	username = $("#regist-username").val();
	if(username.substring(6,12)!='000000'){
		if(username.substring(9,12)=='000'){
			$("#regist-username").val(username.substring(0,12)+'34');
		} else if(username.substring(9,12)!='000'){
			$("#regist-username").val(username.substring(0,12)+'35');
		}
	}
	username = $("#regist-username").val();
	DWREngine.setAsync(false);
	userDao.findUsername(username,function(data){
		if(data==true){
			alert("用户重名!");
			isPass = true;
		}else {
			isPass = false;
		}
	});
	if(isPass){
		DWREngine.setAsync(true);
		return false;
	}
	DWREngine.setAsync(true);
	var password = $('#regist-password').val();
	var repassword = $('#regist-password-confirm').val();
	var fonts = $('.login-regist font');
	var inputs = $('.login-regist input[type=text],.login-regist input[type=password]');
	for(var i=0;i<inputs.length;i++){
		if($(inputs[i]).val()==''){
			$(fonts[i]).css('display','block');
			return false;
		} 
	}
	if(username.substring(12,14)!=34&&username.substring(12,14)!=35){
		alert("aaaaa");
		if(!$('input[type=radio]').is(':checked')){
			$('#regist-type-font').css('display','block');
			return false;
		} else {
			if($("input:radio[name='type']:checked").val()==37){
				if($('#regist-department').val()==-1){
					$('#regist-department-font').css('display','block');
					return false;
				}
			}
		}
	}
	if(password!=''&&repassword!=''){
		if(password!=repassword){
			alert("两次输入密码不一致!");
			return false;
		}
	}
	var reg = /^[1][358]\d{9}$/;
	var tem = $('#regist-phone').val();
	if(!reg.test(tem)){
		$('#regist-phone-font').html("*\t手机号码输入错误,请重新输入");
		$('#regist-phone-font').css('display','block');
		return false;
	}
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var filterValue = $('#regist-email').val();
	if(!filter.test(filterValue)){
		$('#regist-email-font').html("*\t邮箱输入错误,请重新输入");
		$('#regist-email-font').css('display','block');
		return false;
	}
	return true;;
}
